//
//  Contact_DetailHeaderView.m
//  HuaRun2
//
//  Created by admin on 2017/9/9.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Contact_DetailHeaderView.h"
#import "LineView.h"

@interface Contact_DetailHeaderView ()

@property (nonatomic, strong) UIView * backView;

//线1
@property (nonatomic, strong) LineView * line1;
//线2
@property (nonatomic, strong) LineView * line2;
@end

@implementation Contact_DetailHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - life cycle

- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    self.backgroundColor = RGBA(249, 249, 249, 1);
    
    [self addSubview:self.backView];
    [self addSubview:self.nameLab];
    [self addSubview:self.companyLab];
    
    [self addSubview:self.noteView];
    [self addSubview:self.leaveView];
    [self addSubview:self.refuseView];

    [self addSubview:self.sysReportView];

    [self addSubview:self.roleView];
    [self addSubview:self.meetingView];
    [self addSubview:self.giftView];
    
    [self addSubview:self.line1];
    [self addSubview:self.line2];
    
}

- (void)layoutInit {
    weakself(ws);

    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws).offset(kMargin10);
        make.right.mas_equalTo(ws).offset(-kMargin10);
        make.top.mas_equalTo(ws).offset(0);
        make.bottom.mas_equalTo(ws);
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView).offset(kMargin10);
        make.top.mas_equalTo(ws.backView).offset(kMargin15);
        make.right.mas_equalTo(ws.noteView.mas_left).offset(-kMargin10);
    }];

    [self.companyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.nameLab);
        make.top.mas_equalTo(ws.nameLab.mas_bottom).offset(kMargin10);
        make.right.mas_equalTo(ws.sysReportView.mas_left).offset(-kMargin10 * 3);
    }];

    [self.noteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws.backView);
        make.top.mas_equalTo(ws.nameLab);
        make.size.mas_equalTo(CGSizeMake(120, 20));
    }];

    [self.leaveView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.companyLab);
        make.top.mas_equalTo(ws.companyLab.mas_bottom).offset(kMargin10);
        make.size.mas_equalTo(ws.noteView);
    }];

    [self.refuseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.leaveView.mas_right).offset(kMargin10);
        make.top.mas_equalTo(ws.leaveView);
        make.right.mas_equalTo(ws.backView);
    }];

    [self.sysReportView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(ws.companyLab);
        make.top.mas_equalTo(ws.noteView.mas_bottom).offset(kMargin10);
        make.right.mas_equalTo(ws.noteView);
        make.size.mas_equalTo(CGSizeMake(120, 20));
    }];
    CGFloat width = (kMainScreenWidth - kMargin10 * 4 -kMargin10 * 4) / 3;
    [self.meetingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.giftView.mas_right).offset(kMargin10);
        make.bottom.mas_equalTo(ws.backView.mas_bottom).offset(-kMargin15);
        make.size.mas_equalTo(CGSizeMake(width, 25));
    }];

    [self.roleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView.mas_left).offset(kMargin10);
        make.size.bottom.mas_equalTo(ws.meetingView);
    }];

    [self.giftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(ws.backView.mas_centerX).offset(0);;
        make.size.bottom.mas_equalTo(ws.meetingView);
    }];
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.mas_equalTo(ws.roleView);
        make.width.mas_equalTo(1);
        make.left.mas_equalTo(ws.roleView.mas_right).offset(kMargin10);
//        make.centerX.mas_equalTo(ws);
    }];

    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.mas_equalTo(ws.roleView);
        make.width.mas_equalTo(1);
        make.left.mas_equalTo(ws.giftView.mas_right).offset(kMargin10);
    }];

}
#pragma mark - getter

- (UIView *)backView {
    if (!_backView) {
        self.backView = [UIView new];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        self.nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.font = kFontSize15;
        _nameLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
//        _nameLab.text = @"name";
        _nameLab.backgroundColor = kRandomColor;
    }
    return _nameLab;
}

- (UILabel *)companyLab {
    if (!_companyLab) {
        self.companyLab = [UILabel new];
        _companyLab.textAlignment = NSTextAlignmentLeft;
        _companyLab.font = kFontSize15;
        _companyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
//        _companyLab.text = @"company name";
        _companyLab.backgroundColor = kRandomColor;
    }
    return _companyLab;
}
//"rejection_mail"= "拒收邮件";
- (UIView *)refuseView {
    if (!_refuseView) {
        self.refuseView = [self makeCommonLabelWithText:NSLocalizedString(@"rejection_mail", nil)
//                           @"拒收邮件"
                                              imageName:@"拒收邮件"
                           imageSize:(CGSizeMake(15, 15))
                                               viewSize:(CGSizeMake(150, 20))];
        _refuseView.hidden = YES;
    }
    return _refuseView;
}
//"quit_job"= "已离职";
- (UIView *)leaveView {
    if (!_leaveView) {
        self.leaveView = [self makeCommonLabelWithText:NSLocalizedString(@"quit_job", nil)
//                          @"已离职"
                                             imageName:@"已离职"
                                             imageSize:(CGSizeMake(15, 15))
                                              viewSize:(CGSizeMake(100, 20))];
        _leaveView.hidden = YES;
    }
    return _leaveView;
}
- (UIView *)noteView {
    if (!_noteView) {
        self.noteView = [self makeCommonLabelWithText:NSLocalizedString(@"查看备注", nil)
                                            imageName:@"最近查看"
                         imageSize:(CGSizeMake(15, 10))
                         viewSize:(CGSizeMake(120, 20))];
    }
    return _noteView;
}
- (UIView *)sysReportView {
    if (!_sysReportView) {
        self.sysReportView = [self makeCommonLabelWithText:NSLocalizedString(@"其他记录", nil)
                                                 imageName:@"最近查看"
                                                  imageSize:(CGSizeMake(15, 10))
                                                  viewSize:(CGSizeMake(120, 20))];
        _sysReportView.hidden = YES;
    }
    return _sysReportView;
}

- (UIButton *)roleView {
    if (!_roleView) {
        self.roleView = [self makeCommonButtonWithText:NSLocalizedString(@"contacts_rote_count", nil)   normalImage:@"联系人001"];
    }
    return _roleView;
}
- (UIButton *)meetingView {
    if (!_meetingView) {
        self.meetingView = [self makeCommonButtonWithText:NSLocalizedString(@"会议", nil) normalImage:@"会议001"];
    }
    return _meetingView;
}

- (UIButton *)giftView {
    if (!_giftView) {
        self.giftView = [self makeCommonButtonWithText:NSLocalizedString(@"身份", nil) normalImage:@"身份"];
//        self.giftView.hidden = YES;
    }
    return _giftView;
}


- (LineView *)line1 {
    if (!_line1) {
        self.line1 = [LineView new];
    }
    return _line1;
}

- (LineView *)line2 {
    if (!_line2) {
        self.line2 = [LineView new];
//        self.line2.hidden = YES;
    }
    return _line2;
}
- (UIView *)makeCommonLabelWithText:(NSString *)text
                           imageName:(NSString *)imageName
                          imageSize:(CGSize )imageSize
                           viewSize:(CGSize )viewSize{
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = kRandomColor;
    view.frame = CGRectMake(0, 0, viewSize.width, viewSize.height);
    
    UIImageView * iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:imageName];
//    iconView.frame = CGRectMake(0, 2.5, 15, 15);
    iconView.frame = CGRectMake(0, (20 - imageSize.height ) / 2, imageSize.width, imageSize.height);
    [view addSubview:iconView];

    UILabel * companyLab = [UILabel new];
    companyLab.textAlignment = NSTextAlignmentLeft;
    companyLab.frame = CGRectMake(20, 0, viewSize.width - imageSize.width, 20);
    companyLab.font = kGeneralFontSize;
    companyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    companyLab.text = text;
    [view addSubview:companyLab];
    
    return view;
}

- (UIView *)makeCommonLabel2WithText:(NSString *)text
                           imageName:(NSString *)imageName{
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = kRandomColor;
//    view.frame = CGRectMake(0, 0, 100, 30);
    [UIColor whiteColor];
    UIImageView * iconView = [UIImageView new];
    iconView.image = [UIImage imageNamed:imageName];
//    iconView.frame = CGRectMake(0, 7.5, 15, 15);
    [view addSubview:iconView];
    
    UILabel * companyLab = [UILabel new];
    companyLab.textAlignment = NSTextAlignmentLeft;
//    companyLab.frame = CGRectMake(20, 5, 70, 20);
    companyLab.font = kGeneralFontSize;
    companyLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
    companyLab.text = text;
    companyLab.tag = 200;
    [view addSubview:companyLab];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(view);
        make.centerY.mas_equalTo(view);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [companyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconView.mas_right).offset(kMargin10 / 2);
        make.centerY.mas_equalTo(view);
        make.right.mas_equalTo(view);
        make.height.mas_equalTo(20);
    }];
    
    return view;
}
- (void)resetRoleNum:(NSString *)num {
    
    [self commonReSetNum:num view:self.roleView];
}

- (void)commonReSetNum:(NSString *)num view:(UIButton *)view {
    NSString * title ;
    if (view == _roleView) {
        title = NSLocalizedString(@"contacts_rote_count",nil);
//        NSLocalizedString(@"角色", nil);
    }else if (view == _meetingView) {
        title = NSLocalizedString(@"会议", nil);
    }else if (view == _giftView) {
        title = NSLocalizedString(@"身份", nil);
    }
    [view setTitle:[NSString stringWithFormat:@"%@(%@)", title,
                    num]
          forState:(UIControlStateNormal)];
    
}
- (void)resetMeetingNum:(NSString *)num {
    [self commonReSetNum:num view:self.meetingView];
}

- (void)resetGiftNum:(NSString *)num {
    [self commonReSetNum:num view:self.giftView];
}

    - (UIButton *)makeCommonButtonWithText:(NSString *)text
                               normalImage:(NSString *)normalImage {
        
        UIButton * btn = [UIButton new];
        [btn setImage:[UIImage imageNamed:normalImage] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor colorWithHex:@"3c3c3c"] forState:(UIControlStateNormal)];
        [btn setTitleColor:RGBA(243, 161, 50, 1) forState:(UIControlStateSelected)];
        //    btn.backgroundColor = [UIColor redColor];
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitle:text forState:UIControlStateSelected];
        [btn.titleLabel setFont:kGeneralFontSize];
        [btn centerButtonAndImageWithSpacing:kMargin10];
        return btn;
    }

@end
